#include "RingQueue.hpp"
#include "Thread.hpp"
#include <iostream>
#include <vector>
#include <unistd.h>
using namespace ThreadModule;
using ringqueue_t = RingQueue<int>;


void Productor(ringqueue_t& bq)
{
    int t = 10;
    while(true)
    {
        //sleep(1);
        bq.Enqueue(t);
        std::cout << "Product :" << t-- << std::endl;
    }
}

void Consumer(ringqueue_t& bq)
{
    while(true)
    {
        //sleep(1);
        int data;
        bq.Pop(&data);
        std::cout << "Consum :" << data << std::endl;
    }
}

void InitComm(std::vector<Thread<ringqueue_t>>& threads, int num, func_t<ringqueue_t> func, ringqueue_t& bq, const std::string& name)
{
    for (int i = 0; i < num; i++)
    {
        std::string thraedName = name + std::to_string(i + 1);
        threads.emplace_back(func, bq, thraedName);
    }
}

void InitProductor(std::vector<Thread<ringqueue_t>>& threads, int num, ringqueue_t& bq)
{
    InitComm(threads, num, Productor, bq, "productor");
}

void InitConsumer(std::vector<Thread<ringqueue_t>>& threads, int num, ringqueue_t& bq)
{
    InitComm(threads, num, Consumer, bq, "consumer");
}

void StartAllThread(std::vector<Thread<ringqueue_t>>& threads)
{
    for (auto& thread : threads)
        thread.Start();
}

void WaitAllThread(std::vector<Thread<ringqueue_t>>& threads)
{
    for (auto& thread : threads)
        thread.Join();
}

int main()
{
    ringqueue_t* bq = new ringqueue_t(10);
    std::vector<Thread<ringqueue_t>> threads;
    InitProductor(threads, 2, *bq);
    InitConsumer(threads, 3, *bq);
    StartAllThread(threads);
    WaitAllThread(threads);
    return 0;
}